package com.android.multiplesmart.activity;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.databinding.DataBindingUtil;

import com.android.multiplesmart.DemoApplication;
import com.android.multiplesmart.R;
import com.android.multiplesmart.adapter.GridLanguageAdapter;

import com.android.multiplesmart.bean.LanguageDataBean;
import com.android.multiplesmart.bean.LanguageTypeBean;
import com.android.multiplesmart.bean.Servicess;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.schillerchina.baselibrary.constans.AppConstants;
import com.schillerchina.baselibrary.utils.GsonUtil;
import com.schillerchina.baselibrary.utils.Pref;
import com.schillerchina.baselibrary.utils.ToastUtil;
import com.android.multiplesmart.websocket.JWebSocketClient;
import com.android.multiplesmart.websocket.JWebSocketClientService;

import java.lang.reflect.Type;
import java.util.ArrayList;

public class LoginActivity  extends BaseActivity {
    private  EditText editText;
    private  TextView next,skip;
    private JWebSocketClient client;
    private JWebSocketClientService.JWebSocketClientBinder binder;
    private JWebSocketClientService jWebSClientService;
    public String action;

    private boolean isBind = false;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initListener();
    }

    @Override
    public void onClick(View v) {

    }


    @Override
    public void initListener() {

    }

    private void initen(String   name){
        Intent intent=new Intent(this,SelectLanguageActivitys.class);
        intent.putExtra("name",name);
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!checkPermission(this,PERMS_WRITE)){
            requestPermission(this,"Open authority",1,PERMS_WRITE);
        }else{
//            ToastUtil.showToast(this,"Open");
        }
    }
    String message,name,allLaguage;
    private  LanguageTypeBean languageTypeBean;
    private ArrayList<LanguageDataBean> studentLanguages = new ArrayList<>();
    private GridView gridLanguage;
    @Override
    public void initView() {
       setContentView(R.layout.login);
       message=getIntent().getStringExtra("message");
       name=Pref.getString(AppConstants.name,"");
       allLaguage=Pref.getString(AppConstants.allLaguage,"");
       languageTypeBean= GsonUtil.GsonToBean(message,LanguageTypeBean.class);
       gridLanguage=findViewById(R.id.grid_language);
       if (languageTypeBean.getData()!=null){
           if (languageTypeBean.getData().getStudentLanguages() != null) {
               studentLanguages.clear();
               studentLanguages.addAll(languageTypeBean.getData().getStudentLanguages());
               GridLanguageAdapter(studentLanguages);
           }
       }else {
           ToastUtil.showToast("Exception in obtaining translation data");
       }

    }

   private void GridLanguageAdapter(ArrayList<LanguageDataBean> studentLanguages) {
        GridLanguageAdapter gridLanguageAdapter = new GridLanguageAdapter(studentLanguages);
        gridLanguage.setAdapter(gridLanguageAdapter);
        gridLanguage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                LanguageDataBean languageDataBean = (LanguageDataBean) view.getTag();
                Intent intent = new Intent(LoginActivity.this,MainActivitys.class);
                if (name.equals("")){
                    name=languageDataBean.getLanguageEnName()+"_学生1";
                }
//              Servicess.getService().getjWebSClientService().sendMsg("")
                DemoApplication.getHandlerUtils().saveCrashInfo2File("点击LoginActivity语言按钮");
                intent.putExtra(AppConstants.USER_NAME, name);
                intent.putExtra(AppConstants.STUDENT_LANGUAGES, GsonUtil.GsonString(studentLanguages));
                intent.putExtra(AppConstants.SELECT_LANGUAGES, GsonUtil.GsonString(languageDataBean));
                intent.putExtra(AppConstants.TEACHER_CURRICULUM, GsonUtil.GsonString(languageTypeBean));
                intent.putExtra(AppConstants.All_LANGUAGES,allLaguage);
                startActivity(intent);
                finish();
                Log.i("zzg", "languageDataBean:" + languageDataBean.getLanguageName());
            }
        });
    }

    @Override
    public void onBackPressed() {
        // 禁用返回键，不执行任何操作
    }

}
//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        int action = event.getAction();
//        switch (action) {
//            case MotionEvent.ACTION_DOWN:
//                // 处理触摸开始
//                break;
//            case MotionEvent.ACTION_MOVE:
//                // 处理触摸移动
//                break;
//            case MotionEvent.ACTION_UP:
//                // 正常触摸抬起
//                break;
//            // 如果需要强制抬起
//            case FORCE_LIFT_ACTION:
//                // 在这里处理强制抬起的逻辑
//                // 可以发送一个ACTION_UP事件
//                onTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
//                        MotionEvent.ACTION_UP, event.getX(), event.getY(), 0));
//                return true;
//        }
//        return super.onTouchEvent(event);
//    }
//
//    // 假设你有一个常量来表示强制抬起的动作
//    public static final int FORCE_LIFT_ACTION = MotionEvent.ACTION_POINTER_DOWN + 1;
